import axios from 'axios'
import {zhou_for_admin} from '../data'

const STATE = {
  0: {
    type: 'primary',
    text: '出勤'
  },
  1: {
    type: 'warning',
    text: '迟到'
  },
  2: {
    type: 'danger',
    text: '缺勤'
  },
  3: {
    type: 'success',
    text: '请假'
  }
}
export default {
  get_courses_info ({commit, state}){
    axios.post('http://127.0.0.1:5000/course/getCoursesByTeacherName/' + state.teacher).then(
      (res) => {
        commit('get_courses_info', {data: res.data})
      }
    ).catch((reason => {
      console.log(reason)
    }))

  },
  get_class_member({dispatch, commit}, {class_id, course_id}) {
    axios.post('http://127.0.0.1:5000/course/getMemberByClassId/' + class_id).then(
      (res) => {
        setTimeout(
          () => {
            dispatch('prepare_call_info', {class_id, course_id})
          }, 100
        )
        commit('get_class_member', {data: res.data})
      }
    ).catch((reason => {
      console.log(reason)
    }))
  },
  clear_students({commit}) {
    commit('clear_students')
  },
  prepare_call_info({commit, state}, {class_id, course_id}) {
      const students_with_call_info = Array.from(state.students)
      console.log(students_with_call_info)
      students_with_call_info.class_id = class_id
      students_with_call_info.course_id = course_id
      students_with_call_info.forEach((student) => {
        student.s = {
          type: 'primary',
          text: '出勤'
        }
      })
      commit('prepare_call_info', {data: students_with_call_info})
  },
  change_state_of_call_info({commit}, {index, s}) {
    commit('change_state_of_call_info', {index, s})
  },
  change_state_of_call_info_by_z_t_j({commit}, {value}) {
    commit('change_state_of_call_info_by_z_t_j', {value})
  },
  submit_call({commit, state}) {
    const call_record = Array.from(state.students_with_call_info)
    if(state.students_with_call_info.z_t_j === undefined){
      alert('请选择周数再提交')
      return
    }
    const z = state.students_with_call_info.z_t_j[0].split('_')[1]
    const t = state.students_with_call_info.z_t_j[1].split('_')[1]
    const j = state.students_with_call_info.z_t_j[2].split('_')[1]
    call_record.forEach((a_record) => {
      a_record.class_id = state.students_with_call_info.class_id
      a_record.week = z
      a_record.day = t
      a_record.th = j
      a_record.teacher = state.teacher
      const tmpMap = {
        'primary': 0,
        'warning': 1,
        'danger': 2,
        'success': 3
      }
      a_record.state = tmpMap[a_record.s.type]
      a_record.course_id = state.students_with_call_info.course_id
    })
    // console.log(call_record)
    axios.post('http://127.0.0.1:5000/call/submit_call', call_record).then(
      (res) => {
        if(res.data.code === 200){
          alert('本次考勤提交成功！')
          commit('clear_students')
        }else{
          alert('本次考勤提交失败！' + res.data.msg)
        }
      }
    )
  },
  set_record_class({commit}, class_id) {
    commit('set_record_class', {class_id})
  },
  clear_some_data({commit}) {
    commit('clear_some_data')
  },
  set_record_course({commit}, course_id) {
    commit('set_record_course', {course_id})
  },
  get_called_info({commit, state}){
    if(state.students_with_call_info.z_t_j === undefined || state.students_with_call_info === []) {
      alert("请先填写节次再查询考勤！")
      return
    }
    axios.post('http://127.0.0.1:5000/call/getRecordByztj_course_id_class_id',
      {
        z_t_j: state.students_with_call_info.z_t_j,
        course_id: state.sel_course,
        class_id: state.sel_class
      }).then(
      (res) => {
        if(res.data.code === 500){
          const msg = res.data.msg
          alert(msg)
          return
        }
        const d = res.data.data
        let before_at_number = 0
        d.forEach((v) => {
          if(v.s === 0)
            before_at_number += 1
          v.s = STATE[v.s]

        })
        d.before_at_number = before_at_number
        // console.log(d)
        commit('set_called_info', {d})
      }
    ).catch((reason => {
      console.log(reason)
    }))
  },
  fix_call_info({commit, state}) {
    const fix_record = Array.from(state.students_with_call_info)
    if(state.students_with_call_info.z_t_j === undefined){
      alert('请选择周数再提交')
      return
    }
    const z = state.students_with_call_info.z_t_j[0].split('_')[1]
    const t = state.students_with_call_info.z_t_j[1].split('_')[1]
    const j = state.students_with_call_info.z_t_j[2].split('_')[1]
    fix_record.forEach((a_record) => {
      a_record.class_id = state.sel_class
      a_record.week = z
      a_record.day = t
      a_record.th = j
      a_record.before_at_number = state.students_with_call_info.before_at_number
      const tmpMap = {
        'primary': 0,
        'warning': 1,
        'danger': 2,
        'success': 3
      }
      a_record.state = tmpMap[a_record.s.type]
      a_record.course_id = state.sel_course
    })
    // console.log(fix_record)
    axios.post('http://127.0.0.1:5000/call/fix_call', fix_record).then(
      (res) => {
        if(res.data.code === 200){
          alert(res.data.msg)
          commit('clear_students')
        }else{
          alert('本次考勤提交失败！' + res.data.msg)
        }
      }
    )
  },
  set_statistics_z_t({commit}, value) {
    commit("set_statistics_z_t", {data: value})
  },
  get_call_data({commit, state}, row) {
    // 判断z_t
    const z_t = state.statistics_z_t.data
    if (z_t === undefined || z_t.length === 0) {
      alert("添加周数和天数！")
      return
    }
    const z = z_t[0]
    const t = z_t[1]
    let url = ''
    const post_data = {
      z,
      t
    }
    switch (state.statistics_now_state) {
      case 0:
        url = 'http://127.0.0.1:5000/statistics/get_department_call'
        break
      case 1:
        url = 'http://127.0.0.1:5000/statistics/get_major_call'
        post_data.d_id = row.department_id
        state.see_f_id = row.department_id
        break
      case 2:
        url = 'http://127.0.0.1:5000/statistics/get_grade_call'
        post_data.m_id = row.major_id
        state.see_m_id = row.major_id
        break
    }
    axios.post(url,
      post_data).then((res) => {
      if(res.data.code === 200) {
        // console.log(res.data.data)
        commit("set_call_data", {data: res.data.data})
      } else {
        alert(res.data.code + ":" + res.data.msg)
      }
    }).catch((reason) => {

    })
  },
  change_statistics_now_state({commit}) {
    commit("change_statistics_now_state")
  },
  clear_statistics({commit}, clear_something) {
    commit('clear_statistics', {clear_something})
  },
  set_see_options({commit}){
    axios.post('http://127.0.0.1:5000/see/get_d_m_c').then(res=>{
      // console.log(res.data.data)
      const d_m_c = res.data.data
      zhou_for_admin.forEach(z=>{
        z.children.forEach(t=>{
          t.children = d_m_c
        })
      })
      commit("set_see_options", {zhou_for_admin})
    }).catch(reason => {
      console.log(reason)
    })
  },
  get_today_calls({commit, state}) {
    const z_t_j_value_see = state.z_t_j_value_see.data
    if (z_t_j_value_see.length === 0) {
      alert("填写再说！")
      return
    }
    axios.post('http://127.0.0.1:5000/see/get_today_calls', z_t_j_value_see).then(res=>{
      // console.log(res)
      commit('set_today_calls', {data: res.data.data})
    }).catch(reason => {

    })

  },
  clear_see({commit}, clear_something) {
    commit("clear_see", {clear_something})
  },
  get_today_calls_from_statistics({commit, state}, class_id) {
    state.see_grade_id = class_id
    const z_t_j_value_see = [state.statistics_z_t.data[0], state.statistics_z_t.data[1], state.see_f_id, state.see_m_id, state.see_grade_id]
    state.z_t_j_value_see.data = z_t_j_value_see
    // console.log(z_t_j_value_see);
    axios.post('http://127.0.0.1:5000/see/get_today_calls', z_t_j_value_see).then(res=>{
      console.log(res)
      commit('set_today_calls', {data: res.data.data})
    }).catch(reason => {

    })
  }
}
